package cn.cxl.utils;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.text.TextUtils;

import java.util.List;

/**
 * ====================================================
 * <p>
 * 作者：CXL
 * <p>
 * 创建时间：2022/9/15 9:53
 * ====================================================
 */
public class OpenApp {
    public static Intent getAppOpenIntentByPackageName(Context context, String packageName){

        String mainAct = null;

        PackageManager pkgMag = context.getPackageManager();

        Intent intent = new Intent(Intent.ACTION_MAIN);

        intent.addCategory(Intent.CATEGORY_LAUNCHER);

        intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED|Intent.FLAG_ACTIVITY_NEW_TASK);

        @SuppressLint("WrongConstant") List<ResolveInfo> list = pkgMag.queryIntentActivities(intent,

                PackageManager.GET_ACTIVITIES);

        for (int i = 0; i < list.size(); i++) {

            ResolveInfo info = list.get(i);

            if (info.activityInfo.packageName.equals(packageName)) {

                mainAct = info.activityInfo.name;

                break;

            }

        }

        if (TextUtils.isEmpty(mainAct)) {

            return null;

        }

        intent.setComponent(new ComponentName(packageName, mainAct));

        return intent;

    }
}
